package cn.yitun.chat.yitunservermanagecore.general.manage.common.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yituncommonweb.util.YitunHttpUtil;
import cn.yitun.chat.yitungeneral.annotation.parameter.ParamDefine;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.util.IpUtil;
import cn.yitun.chat.yitunservercommonlib.util.PhoneUtil;
import cn.yitun.chat.yitunservercommonlib.util.PinYinUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具类，被小程序调用api
 * 
 * @author admin
 * @email chen@163.com
 * @date 2020-11-06 15:16:13
 */
 
@RestController
@RequestMapping("/manage/util")
public class UtilController extends BaseController {

	@PermissionMapping(name = "ip地址", isIntercept = false)
	@RequestMapping(method = RequestMethod.GET, value = "/ip_location")
	public ResultMessage getIp(HttpServletRequest request, @ParamDefine("ip") String ip) {
		String otherIp = YitunHttpUtil.getIpAddr(request);
		if (YitunStringUtil.isEmpty(ip))
			throw new YitunException(ExceptionEnum.BUSY_SERVER);
		return new ResultMessage(R.param("ipLocation", IpUtil.getCityInfo(ip)).put("otherIp", otherIp));
	}


	@PermissionMapping(name = "手机号归属地", isIntercept = false)
	@RequestMapping(method = RequestMethod.GET, value = "/phone_region")
	public ResultMessage getRegionCode(HttpServletRequest request, @ParamDefine("phoneNumber") String phoneNumber,
									   @ParamDefine("countryCode") String countryCode) {

		return new ResultMessage(
				R.param("phoneRegion", PhoneUtil.getPhoneRegionCode(phoneNumber, countryCode))
						.put("phoneNumber", phoneNumber)
						.put("countryCode", countryCode)
		);
	}

	@PermissionMapping(name = "汉语拼音", isIntercept = false)
	@RequestMapping(method = RequestMethod.GET, value = "/get_pin_yin")
	public ResultMessage getPinYin(HttpServletRequest request, @ParamDefine("chineseWords") String chineseWords) {

		return new ResultMessage(
				R.param("pinyin", PinYinUtil.getFullSpell(chineseWords))
		);
	}

}
